package com.intellagric.service.search.Impl.agriecom;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.service.search.agriecom.AgriecomNewsSearch;
import com.intellagric.service.search.agriecom.pojo.AgriecomNewsResult;
import com.intellagric.service.search.dao.agriecom_news.AgriecomNewsSearchDao;
import com.intellagric.service.search.mapper.AgriecomNewsSearchMapper;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class AgriecomNewsSearchImpl implements AgriecomNewsSearch {

    @Autowired
    private AgriecomNewsSearchMapper searchMapper;

    @Autowired
    @Qualifier("SolrServer3")
    private HttpSolrServer solrServer;



    @Autowired
    private AgriecomNewsSearchDao searchDao;


    /**
     * 从数据库查出数据并提交到索引库
     * @return
     */
    @Override
    public LayuiDataGridResult getNewsList() {
        List<AgriecomNewsResult> resultList=searchMapper.getNewsList();
        if(resultList!=null) {

            for (AgriecomNewsResult result:resultList){
                SolrInputDocument document=new SolrInputDocument();
                document.setField("id",result.getId());
                document.setField("news_title",result.getTitle());
                document.setField("source",result.getSource());
                document.setField("remarks",result.getRemarks());
                document.setField("brief",result.getBrief());
                document.setField("create_date",result.getCreateDate());
                document.setField("is_hot",result.getIsHot());
                document.setField("news_detail",result.getNewsDetail());
                document.setField("news_type_name",result.getNewsTypeName());
                try {
                    solrServer.add(document);

                } catch (SolrServerException e) {
                    e.printStackTrace();
                    return LayuiDataGridResult.fail();
                } catch (IOException e) {
                    e.printStackTrace();
                    return LayuiDataGridResult.fail();
                }
            }
            try {
                solrServer.commit();
            } catch (SolrServerException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return LayuiDataGridResult.success();
        }
        else
            return LayuiDataGridResult.fail();
    }

    /**
     * title关键字查询
     * @param keyword
     * @param limit
     * @param page
     * @return
     */
    @Override
    public LayuiDataGridResult titleSearch(String keyword, int limit, int page) {
        SolrQuery query=new SolrQuery();
        query.set("q",keyword);
        //设置分页条件
        query.setStart((page-1)*limit);
        query.setRows(limit);
        //设置默认搜索域
        query.set("df","news_title");
        //设置高亮
        query.setHighlight(true);
        query.addHighlightField("news_title");
        //设置高亮形式
        query.setHighlightSimplePre("<em style=\"color:red\">");
        query.setHighlightSimplePost("</em>");
        try {
            return searchDao.query(query);
        } catch (SolrServerException e) {
            e.printStackTrace();
            return LayuiDataGridResult.fail();
        }
    }

    /**
     *  根据新闻id添加到索引库
     * @param id
     * @return
     */
    public ResponseMessage addNews(String id){
        AgriecomNewsResult result=searchMapper.selectByPrimaryKey(id);
        return searchDao.add(result);
    }
}
